package com.atsansan.mywork.login.controller;

import com.atsansan.mywork.login.entity.BookEntity;
import com.atsansan.mywork.login.entity.CourseraEntity;
import com.atsansan.mywork.login.service.CourseraService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author sansan
 * @create 2021-03-12 18:17
 */
@Controller
@RequestMapping("user/coursera")
public class CourseraController {

    @Autowired
    CourseraService courseraService;

    @RequestMapping("/listC")
    public String list(Map<String,Object> map){
        List<CourseraEntity> courseraList = courseraService.list();
        map.put("courseraList",courseraList);
//        System.out.println(list.toString());
        return "listC";
    }
    @RequestMapping(value = {"/select/list/coursera"})
    public String selectListCoursera(@RequestParam("cname") String key, Map<String,Object> map){


        List<CourseraEntity> listSelectCoursera = courseraService.listByKey(key);
        map.put("listSelectCoursera",listSelectCoursera);

        return "listC";
    }
    @RequestMapping(value = {"/select/coursera"})
    public String selectList(@RequestParam("cname") String key, Map<String,Object> map){


            List<CourseraEntity> listByKeyCoursera = courseraService.listByKey(key);
            map.put("listByKeyCoursera",listByKeyCoursera);

        return "login_success";
    }
    @RequestMapping("/update/{cId}")
    public String updateById(@PathVariable("cId") Long cId,Map<String,Object> map){
        CourseraEntity courseraEntity = courseraService.getById(cId);
        map.put("courseraEntity",courseraEntity);
        return "updateC";
    }

    @RequestMapping("updateC")
    public String update(CourseraEntity courseraEntity){
        courseraService.updateById(courseraEntity);
        return "redirect:/user/coursera/listC";
    }
    @DeleteMapping("/delete/{cId}")
    public String delete(@PathVariable("cId") Long cId){

        courseraService.removeById(cId);
        return "redirect:/user/coursera/listC";
    }
}
